<?php


namespace api\controllers;


use api\components\BaseController;
use common\helpers\commonApi;
use common\models\Cart;
use Yii;

/**
 * 购物车
 * Class CartController
 * @package api\controllers
 */
class CartController extends BaseController
{
    public $model = null;
    public function init()
    {
        parent::init();
        $this->model = new Cart();
    }
    /**
     * 加入购物
     * @return array
     */
    public function actionJoin()
    {
        $post = Yii::$app->request->post();
        $good_id = $post['g_id']??'';
        $spec_id = $post['spec_id']??'';
        $num = $post['num']??1;
        return $this->model->join(self::$userid,$good_id,$spec_id,$num,$this->campus_id);
    }

    /**
     * 购物车列表
     * @return array
     */
    public function actionList()
    {
        return $this->model->getList(self::$userid,$this->campus_id);
    }

    /**
     * 加减
     * @return array
     */
    public function actionAddSub()
    {
        $post = Yii::$app->request->post();
        if (empty($post['cart_id'])){
            return commonApi::message('请选择要删除的商品',false);
        }
        if (!isset($post['type'])){
            return commonApi::message('参数错误',false);
        }
        return $this->model->addSub(self::$userid,$post['cart_id'],$this->campus_id,$post['type']);
    }

    /**
     * 删除
     * @return array
     */
    public function actionDel()
    {
        $get = Yii::$app->request->get();
        if (empty($get['cart_id'])){
            return commonApi::message('请选择要删除的商品',false);
        }
        return $this->model->del(self::$userid,$get['cart_id']);
    }

    /**
     * 购物车数量
     * @return array
     */
    public function actionNum()
    {
        return $this->model->num(self::$userid,$this->campus_id);
    }
}